package controller;

import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import pojo.Products;
import service.AdminProductsService;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
public class AdminProducts {
    @RequestMapping("/toAdd")
    public String initAdd(Model model){
        List<Products> p = adminProductsService.sAP();
        model.addAttribute("p",p);
        return "adminAddProductsPage";}
    @Autowired
    private AdminProductsService adminProductsService;
    @RequestMapping("/addProducts")
    public String addProducts(HttpServletRequest request,MultipartFile file,Products products, ModelMap map, Model model)throws IOException{
        String name = UUID.randomUUID().toString().replaceAll("-", "");
        String ext = FilenameUtils.getExtension(file.getOriginalFilename());
        String url = request.getSession().getServletContext().getRealPath("/img");
        System.out.println(url);
        file.transferTo(new File(url+"/"+name + "." + ext));
        products.setPimg("img/"+name + "." + ext);
        int a = adminProductsService.aP(products);
        if (a>0){
            List<Products> p = adminProductsService.sAP();
            model.addAttribute("p",p);
            map.addAttribute("products",products);
            model.addAttribute("msg","添加成功！");
            return "adminAddProductsPage";
        }
        List<Products> p = adminProductsService.sAP();
        model.addAttribute("p",p);
        model.addAttribute("msg","添加失败");
        return "adminAddProductsPage";
    }
    @RequestMapping("/toDel")
    public String initDel(Model model){
        List<Products> p = adminProductsService.sAP();
        model.addAttribute("p",p);
        return "adminDeleteProductsPage";}
    @RequestMapping("/deleteProducts")
    public String deleteProducts(Integer pid,Model model){
        int a=adminProductsService.dP(pid);
        if(a>0){
            List<Products> p = adminProductsService.sAP();
            model.addAttribute("p",p);
            model.addAttribute("msg","删除成功！");
            return "adminDeleteProductsPage";
        }
            List<Products> p = adminProductsService.sAP();
            model.addAttribute("p",p);
            model.addAttribute("msg","删除失败！");
            return "adminDeleteProductsPage";
    }
    @RequestMapping("/toUp")
    public String initUp(Model model){
        List<Products> p = adminProductsService.sAP();
        model.addAttribute("p",p);
        return "adminUpdateProducts";}
    @RequestMapping("/toUpdate")
    public String toUpdate(Integer pid,Model model){
        List<Products> p = adminProductsService.sOP(pid);
        model.addAttribute("p",p);
        return "adminUpdatePros";
    }
    @RequestMapping("/updateProducts")
    public String updateProducts(Products products,HttpServletRequest request, Model model,MultipartFile file,ModelMap map) throws IOException {
        System.out.println(products.toString());
        if (file.isEmpty()){
            System.out.println("123");
            products.setPimg(products.getPimg());
            int a = adminProductsService.uP(products);
            if (a>0){
                List<Products> p = adminProductsService.sAP();
                model.addAttribute("p",p);
                map.addAttribute("products",products);
                model.addAttribute("msg","修改成功！");
                return "adminUpdateProducts";
            }
            List<Products> p = adminProductsService.sAP();
            model.addAttribute("p",p);
            model.addAttribute("msg","修改失败");
            return "adminUpdateProducts";
        }
            String name = UUID.randomUUID().toString().replaceAll("-", "");
            String ext = FilenameUtils.getExtension(file.getOriginalFilename());
            String url = request.getSession().getServletContext().getRealPath("/img");
            System.out.println(url);
            file.transferTo(new File(url+"/"+name + "." + ext));
            products.setPimg("img/"+name + "." + ext);
            int a = adminProductsService.uP(products);
            if (a>0){
                List<Products> p = adminProductsService.sAP();
                model.addAttribute("p",p);
                map.addAttribute("products",products);
                model.addAttribute("msg","修改成功！");
                return "adminUpdateProducts";
            }
            List<Products> p = adminProductsService.sAP();
            model.addAttribute("p",p);
            model.addAttribute("msg","修改失败");
            return "adminUpdateProducts";
    }
    @RequestMapping("/selectAllProducts")
    public String selectAllProducts(Model model){
        List<Products> products=adminProductsService.sAP();
        System.out.println(products);
        model.addAttribute("products",products);
        return "adminSelectAllProducts";
    }
    @RequestMapping("/search")
    public String search(Model model){
        List<Products> pro=adminProductsService.sAP();
        System.out.println(pro);
        model.addAttribute("pro",pro);
        return "adminSearchProducts";
    }
    @RequestMapping("/toSelectOne")
    public String initSelect(){return "adminSelectOneProduct";}
    @RequestMapping("/selectOneProducts")
    public String selectOneProduct(Integer pid,Model model){
        List<Products> p=adminProductsService.sOP(pid);
        model.addAttribute("p",p);
        if(p.size()!=0){
            List<Products> pp=adminProductsService.sOP(pid);
            model.addAttribute("p",pp);
            return "adminSelectOneProduct";
        }
        model.addAttribute("msg","不存在的商品编号，没有找到此商品");
        return "adminSelectOneProduct";
    }

}
